//
//  SHAlertTableVC.m
//  SHWheel
//
//  Created by YYKJ on 2020/7/29.
//  Copyright © 2020 YYKJ. All rights reserved.
//

#import "SHAlertTableVC.h"
#import "SHTextFieldAlertController.h"
#import "SHUIModalPresentationViewController.h"
#import <QMUIKit/QMUIAlertController.h>
#import <QMUIKit/QMUITextField.h>

@interface SHAlertTableVC ()
@property (nonatomic, copy) NSArray *titles;
@end

@implementation SHAlertTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titles = @[@"Custom alert",
                    @"System alert",
                    @"QMUI alert",
                    @"Textfield custom alert",];
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"reusedID"];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusedID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titles[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titles[indexPath.row];
    if ([title isEqualToString:@"Custom alert"]) {
        [self handleWindowShowing];
    } else if ([title isEqualToString:@"System alert"]) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"true" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertVC animated:YES completion:nil];
    } else if ([title isEqualToString:@"QMUI alert"]) {
        QMUIAlertController *alertController = [[QMUIAlertController alloc] initWithTitle:@"title" message:@"message" preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction: [QMUIAlertAction actionWithTitle:@"ok" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
             
        }]];
        [alertController showWithAnimated:YES];
    } else if ([title isEqualToString:@"Textfield custom alert"]) {
        SHTextFieldAlertController *alertController = [[SHTextFieldAlertController alloc] init];
        [alertController showWithAnimated:YES];
    }
}

- (void)handleWindowShowing {
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 208)];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = 6;
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 24;
    paragraphStyle.maximumLineHeight = 24;
    paragraphStyle.paragraphSpacing = 16;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
                                                   initWithString:@"QMUIModalPresentationViewController支持 3 种使用方式，当前使用第 1 种。提供 UIWindow 的方式目的是为了盖住状态栏，但 iOS 13 及以后，状态栏已经无法被盖住了，所以只有在 iOS 12 及以下才能看到状态栏盖住的效果。"
                                                   attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16],
                                                                NSForegroundColorAttributeName: UIColor.grayColor,
                                                                NSParagraphStyleAttributeName: paragraphStyle}];
    label.attributedText = attributedString;
    [contentView addSubview:label];
    
    UIEdgeInsets contentViewPadding = UIEdgeInsetsMake(20, 20, 20, 20);
    label.frame = CGRectMake(contentViewPadding.left, contentViewPadding.top, CGRectGetWidth(contentView.bounds) - UIEdgeInsetsGetHorizontalValue(contentViewPadding), CGFLOAT_MAX);
    
    SHUIModalPresentationViewController *modalViewController = [[SHUIModalPresentationViewController alloc] init];
    modalViewController.contentView = contentView;
    // 以 UIWindow 的形式来展示
    [modalViewController showWithAnimated:YES completion:nil];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


@end
